package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.GroupBrandVo;
import com.cskaoyan.bean.vo.GroupCategoryVo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.util.MyBatisUtil;
import com.cskaoyan.util.TransactionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GroupGoodsManagementServiceImpl implements GroupGoodsManagementService {

//    private GroupBrandService groupBrandService = new GroupBrandServiceImpl();
//    private GroupCategoryService groupCategoryService = new GroupCategoryServiceImpl();
//
//    private GroupGoodsService groupGoodsService = new GroupGoodsServiceImpl();
//    private GroupSpecificationService groupSpecificationService = new GroupSpecificationServiceImpl();
//    private GroupGoodsAttributeService attributeService = new GroupGoodsAttributeServiceImpl();
//    private GroupGoodsProductService productService = new GroupGoodsProductServiceImpl();

//    @Override
//    public BaseRespVo create(GoodsVo goodsVo) {
//        TransactionUtil.startTransaction();
//        try {
//            BaseRespVo error = validate(goodsVo);
//            if (error != null) {
//                return error;
//            }
//
//            MarketGoods goods = goodsVo.getGoods();
//            MarketGoodsAttribute[] attributes = goodsVo.getAttributes();
//            MarketGoodsSpecification[] specifications = goodsVo.getSpecifications();
//            MarketGoodsProduct[] products = goodsVo.getProducts();
//
//            String name = goods.getName();
//            if (this.checkExistByName(name)) {
//                return BaseRespVo.fail(611, "商品名已经存在");
//            }
//
//            // 商品表里面有一个字段retailPrice记录当前商品的最低价
//            BigDecimal retailPrice = new BigDecimal(Integer.MAX_VALUE);
//            for (MarketGoodsProduct product : products) {
//                BigDecimal productPrice = product.getPrice();
//                if(retailPrice.compareTo(productPrice) == 1){
//                    retailPrice = productPrice;
//                }
//            }
//            goods.setRetailPrice(retailPrice);
//
//            // 商品基本信息表market_goods
//            this.add(goods);
//
//
//            // 商品规格表market_goods_specification
//            for (MarketGoodsSpecification specification : specifications) {
//                specification.setGoodsId(goods.getId());
//                specification.setId(null);
//                specificationService.add(specification);
//            }
//
//            // 商品参数表market_goods_attribute
//            for (MarketGoodsAttribute attribute : attributes) {
//                attribute.setGoodsId(goods.getId());
//                attribute.setId(null);
//                attributeService.add(attribute);
//            }
//
//            // 商品货品表market_product
//            for (MarketGoodsProduct product : products) {
//                product.setId(null);
//                product.setGoodsId(goods.getId());
//                productService.add(product);
//            }
//        } catch (Exception exception) {
//            exception.printStackTrace();
//            TransactionUtil.rollback();
//        }
//        TransactionUtil.commit();
//        return BaseRespVo.ok();
//    }
//
//    @Override
//    public boolean checkExistByName(String name) {
//        SqlSession sqlSession = MyBatisUtil.getSqlSession();
//        MarketGoodsMapper goodsMapper = sqlSession.getMapper(MarketGoodsMapper.class);
//        MarketGoodsExample example = new MarketGoodsExample();
//        example.or().andNameEqualTo(name).andIsOnSaleEqualTo(true).andDeletedEqualTo(false);
//        boolean b = goodsMapper.countByExample(example) != 0;
//        sqlSession.close();
//        return b;
//    }
//
//    @Override
//    public int add(MarketGoods goods) {
//        MarketGoodsMapper goodsMapper = TransactionUtil.getMapper(MarketGoodsMapper.class);
//        Date now = new Date();
//        goods.setAddTime(now);
//        goods.setUpdateTime(now);
//        return goodsMapper.insertSelective(goods);
//    }


    @Override
    public CommonData groupGoodsQuery(Integer page, Integer limit, String sort, String order,String goodsId,String name,String goodsSn) {
        MarketGoodsMapper marketGoodsMapper = MyBatisUtil.getMapper(MarketGoodsMapper.class);
        PageHelper.startPage(page, limit);

        MarketGoodsExample example = new MarketGoodsExample();

//        MarketGoodsExample.Criteria criteria = example.createCriteria();//List<条件>
        if(goodsId.isEmpty()&&goodsSn.isEmpty()&&name.isEmpty()){
            example.or();
        }if (!goodsId.isEmpty()) {
            example.createCriteria().andIdEqualTo(Integer.parseInt(goodsId));
        }if (!goodsSn.isEmpty()) {
            example.createCriteria().andGoodsSnLike("%"+goodsSn+"%");
        } if (!name.isEmpty()) {
            example.createCriteria().andNameLike("%"+name+"%");
        }


        List<MarketGoods> goods = marketGoodsMapper.selectByExampleWithBLOBs(example);

        // 3获得分页信息并封装
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(goods);
        CommonData data = CommonData.data(pageInfo);
        return data;
    }


    @Override
    public void groupDeleteGoods(MarketGoods goods) {
        SqlSession session = TransactionUtil.getSqlSession();

        int marketGoodsId = goods.getId();

        //删除MarketGoodsSpecification中的数据
        MarketGoodsSpecificationExample example01 = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationMapper marketGoodsSpecificationMapper = session.getMapper(MarketGoodsSpecificationMapper.class);
        example01.createCriteria().andGoodsIdEqualTo(marketGoodsId);
        int a = marketGoodsSpecificationMapper.deleteByExample(example01);


        //删除marketGoodsAttribute中的数据
        MarketGoodsAttributeExample example02 = new MarketGoodsAttributeExample();
        MarketGoodsAttributeMapper marketGoodsAttributeMapper = session.getMapper(MarketGoodsAttributeMapper.class);
        example02.createCriteria().andGoodsIdEqualTo(marketGoodsId);
        int b = marketGoodsAttributeMapper.deleteByExample(example02);


        //删除MarketGoods表中的数据
        MarketGoodsMapper marketGoodsMapper = session.getMapper(MarketGoodsMapper.class);
        int c = marketGoodsMapper.deleteByPrimaryKey(marketGoodsId);


        TransactionUtil.commit();
    }

    @Override
    public BaseRespVo catAndBrandsList() {
//        Map<List<MarketBrand>, List<MarketCategory>> result = new HashMap<>();
//
//        SqlSession session = MyBatisUtil.getSqlSession();
//
//        MarketCategoryMapper marketCategoryMapper = session.getMapper(MarketCategoryMapper.class);
//        MarketCategoryExample example01 = new MarketCategoryExample();
//        example01.or();
//        List<MarketCategory> marketCategoryList = marketCategoryMapper.selectByExample(example01);
//
//        MarketBrandMapper marketBrandMapper = session.getMapper(MarketBrandMapper.class);
//        MarketBrandExample example02 = new MarketBrandExample();
//        example02.or();
//        List<MarketBrand> marketBrandList = marketBrandMapper.selectByExample(example02);
//
//        result.put(marketBrandList, marketCategoryList);


        Map<String, Object> data = new HashMap<>();
        List<GroupCategoryVo> categoryList = new ArrayList<>();
        List<GroupBrandVo> brandList = new ArrayList<>();


        SqlSession session = MyBatisUtil.getSqlSession();

        MarketCategoryMapper categoryMapper = session.getMapper(MarketCategoryMapper.class);
        List<MarketCategory> categoryLst = categoryMapper.selectByExample(null);
        for (MarketCategory marketCategory : categoryLst) {
            GroupCategoryVo category = new GroupCategoryVo();
            category.setValue(marketCategory.getId()); // 分类ID
            category.setLabel(marketCategory.getName()); // 分类名称
            categoryList.add(category);
        }

        MarketBrandMapper brandMapper = session.getMapper(MarketBrandMapper.class);
        List<MarketBrand> brandLst = brandMapper.selectByExample(null);


        for (MarketBrand marketBrand : brandLst) {
            GroupBrandVo brand = new GroupBrandVo();
            brand.setValue(marketBrand.getId());  // 品牌ID
            brand.setLabel(marketBrand.getName());  // 品牌名称
            brandList.add(brand);
        }

        data.put("categoryList", categoryList);
        data.put("brandList", brandList);
        session.close();
        return BaseRespVo.ok(data);


    }


    //=================================================================
//        List<Map<Integer, String>> a = new ArrayList<>();
//        for (Map<Integer, String> integerStringMap : a) {
//            for (MarketBrand marketBrand : marketBrandList) {
//                integerStringMap.put(marketBrand.getId(), marketBrand.getName());
//            }
//        }
//
//        Map<Integer, String> b = new HashMap<>();
//        for (MarketCategory marketCategory : marketCategoryList) {
//            b.put(marketCategory.getId(), marketCategory.getName());
//        }
//        MarketCategoryExample example04 = new MarketCategoryExample();
//        example04.createCriteria().andPidEqualTo(0);
//        List<MarketCategory> Pid0 = marketCategoryMapper.selectByExample(example04);
//        Map<List<MarketCategory>, Map<Integer, String>> c = new HashMap<>();
//        c.put(Pid0, b);
//        Map<List<Map<Integer, String>>, Map<List<MarketCategory>, Map<Integer, String>>> result33 = new HashMap<>();
//        result33.put(a,c);


}


